package com.weidong.poidemo.controller;

import com.weidong.poidemo.service.ExcelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author weidong
 * @version 1.0
 * @date 2017/11/18 14:29
 * @description
 */
@RestController
public class ExcelController {

    @Autowired
    private ExcelService excelService;

    /**
     * 将数据表的数据导出到本地excel中(手动实现)
     * @param response
     * @throws Exception
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
       excelService.export(response);
    }

    /**
     * 将数据表的数据导出到本地excel中并通过读取模板样式(手动实现)
     * @param response
     * @throws Exception
     */
    @GetMapping("/exportTemple")
    public void exportTemple(HttpServletResponse response) throws Exception {
        excelService.exportTemple(response);
    }

    /**
     * 将数据表的数据导出到本地excel中(通过工具类)
     * @param response
     * @throws Exception
     */
    @GetMapping("/excelExportUtil")
    public void excelExportUtil(HttpServletResponse response) throws Exception {
        excelService.excelExportUtil(response);
    }

    /**
     * 将excel的数据读取到数据表(手动实现)
     * @param file
     * @throws Exception
     */
    @PostMapping("/save")
    public void save(@RequestParam(name="file") MultipartFile file) throws Exception {
        excelService.save(file);
    }

    /**
     * 将excel的数据读取到数据表(通过工具类)
     * @param response
     * @param file
     * @throws Exception
     */
    @PostMapping("/importExcel")
    public void importExcel(HttpServletResponse response,@RequestParam(name="file") MultipartFile file) throws Exception {
        excelService.importExcel(response,file);
    }

}
